package com.pr.makfilm.user.pages.school.list.cmp;

import com.pr.makfilm.core.cmp.table.AjaxDataTable;
import com.pr.makfilm.core.cmp.table.DataProvider;
import com.pr.makfilm.core.cmp.table.SelectionChangeCallback;
import com.pr.makfilm.domain.District;
import com.pr.makfilm.domain.ObjectStateEnum;
import com.pr.makfilm.domain.filter.DistrictFilter;
import com.pr.makfilm.domain.filter.OpenSearchDescription;
import com.pr.makfilm.security.service.ISecurityService;
import com.pr.makfilm.service.IDistrictService;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Balbina on 12.12.13.
 */
public class DistrictTab extends Panel
{
   @SpringBean
   private IDistrictService districtService;

   @SpringBean
   private ISecurityService securityService;

   private AjaxDataTable<District> table;

   private District selected;

   private DistrictFilterToolbar filterToolbar;

   public DistrictTab(String id)
   {
      super(id);
   }

   @Override
   protected void onInitialize()
   {
      super.onInitialize();
      List<IColumn<District, String>> columns = new ArrayList<IColumn<District, String>>();
      columns.add(new PropertyColumn<District, String>(
              new ResourceModel("name"),
              District.FIELD_NAME,
              District.FIELD_NAME
      ));
      columns.add(new PropertyColumn<District, String>(
              new ResourceModel("description"),
              District.FIELD_DESCRIPTION,
              District.FIELD_DESCRIPTION
      ));

      OpenSearchDescription<District> osd = new OpenSearchDescription<District>();
      DistrictFilter filter = new DistrictFilter();
      filter.setObjectState(ObjectStateEnum.ACTIVE);
      filter.setUser(securityService.getLoggedInUser());
      osd.setFilter(filter);
      DataProvider<District> dataProvider = new DataProvider<District>(districtService, osd);
      table = new AjaxDataTable<District>("table", columns, dataProvider);

      table.setSelectionChangeCallback(new SelectionChangeCallback<District>()
      {
         @Override
         public void onSelectionChange(AjaxRequestTarget target, IModel<District> model)
         {
            selected = model.getObject();
            refreshTableView(target);
         }
      });
      table.addFilterToolbar(filterToolbar = new DistrictFilterToolbar(table, new PropertyModel<District>(this, "selected")));
      add(table);
   }

   private void refreshTableView(AjaxRequestTarget target)
   {
      target.add(table);
//      target.add(filterToolbar);
   }
}
